package com.ruoyi.activity.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.activity.model.domain.Evaluation;
import com.ruoyi.activity.model.domain.Work;
import com.ruoyi.activity.service.IWorkService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.activity.service.IEvaluationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作品评价信息Controller
 *
 * @author ruoyi
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/activity/evaluation")
public class EvaluationController extends BaseController {
    @Autowired
    private IEvaluationService evaluationService;

    @Autowired
    private IWorkService workService;

    /**
     * 查询作品评价信息列表
     */
    @PreAuthorize("@ss.hasPermi('activity:evaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Evaluation evaluation) {
        startPage();
        List<Evaluation> list = evaluationService.selectEvaluationList(evaluation);
        return getDataTable(list);
    }

    /**
     * 获取作品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:work:query')")
    @GetMapping(value = "/work/{id}")
    public AjaxResult getWorkInfo(@PathVariable("id") Long id) {
        if (id == null) {
            return null;
        }
        Evaluation evaluation = evaluationService.selectEvaluationById(id);
        if (evaluation == null) {
            return null;
        }
        Long workId = evaluation.getWorkId();
        Work work = workService.selectWorkById(workId);
        return success(work);
    }

    /**
     * 导出作品评价信息列表
     */
    @PreAuthorize("@ss.hasPermi('activity:evaluation:export')")
    @Log(title = "作品评价信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Evaluation evaluation) {
        List<Evaluation> list = evaluationService.selectEvaluationList(evaluation);
        ExcelUtil<Evaluation> util = new ExcelUtil<Evaluation>(Evaluation.class);
        util.exportExcel(response, list, "作品评价信息数据");
    }

    /**
     * 获取作品评价信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:evaluation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(evaluationService.selectEvaluationById(id));
    }

    /**
     * 新增作品评价信息
     */
    @PreAuthorize("@ss.hasPermi('activity:evaluation:add')")
    @Log(title = "作品评价信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Evaluation evaluation) {
        return toAjax(evaluationService.insertEvaluation(evaluation));
    }

    /**
     * 修改作品评价信息
     */
    @PreAuthorize("@ss.hasPermi('activity:evaluation:edit')")
    @Log(title = "作品评价信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Evaluation evaluation) {
        return toAjax(evaluationService.updateEvaluation(evaluation));
    }

    /**
     * 删除作品评价信息
     */
    @PreAuthorize("@ss.hasPermi('activity:evaluation:remove')")
    @Log(title = "作品评价信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(evaluationService.deleteEvaluationByIds(ids));
    }
}
